package com.situ.student.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.student.dao.IKechengDao;
import com.situ.student.entity.Kecheng;
import com.situ.student.util.JDBCUtil;

public class KechengDaoImpl implements IKechengDao {

	@Override
	public List<Kecheng> list() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Kecheng> list = new ArrayList<>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,kecheng_name,xuefen from kecheng";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer id = resultSet.getInt("id");
				String kechengname = resultSet.getString("kecheng_name");
				Integer xuefen = resultSet.getInt("xuefen");
				Kecheng kecheng = new Kecheng(id, kechengname, xuefen);
				list.add(kecheng);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		System.out.println("kechenglist=" + list);
		return list;

	}

	// 根据ID删除
	@Override
	public int deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from kecheng where id=?";
			// 预编译
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			count = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}

		return count;
	}

	@Override
	public Kecheng findById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Kecheng kecheng = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select * from kecheng where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				Integer kechengid = resultSet.getInt("id");//括号内的是表格的字段名
				String kechengname = resultSet.getString("kecheng_name");
				Integer xuefen = resultSet.getInt("xuefen");
				kecheng = new Kecheng(kechengid, kechengname, xuefen);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}

		return kecheng;

	}

	@Override
	public int update(Kecheng kecheng) {
		Connection connection = null;
		ResultSet resultSet = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update kecheng set kecheng_name=?,xuefen=? where id=?";
			// 预编译
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, kecheng.getKechengname());
			preparedStatement.setInt(2, kecheng.getXuefen());
			preparedStatement.setInt(3, kecheng.getId());
			// 返回影响的行数
			count = preparedStatement.executeUpdate();
			System.out.println("count=" + count);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return count;
	}

	@Override
	public int insert(Kecheng kecheng) {
		Connection connection=null;
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		int count=0;
		try {
			connection=JDBCUtil.getConnection();
			String sql="insert into kecheng(kecheng_name,xuefen) values (?,?)";
			//预编译
			preparedStatement=connection.prepareStatement(sql);
			preparedStatement.setString(1, kecheng.getKechengname());
			preparedStatement.setInt(2, kecheng.getXuefen());
			count=preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return count;
	}

}
